package mirrormonkey.core.server;

import mirrormonkey.framework.entity.SyncEntity;

import com.jme3.network.MessageConnection;

public class ExistingMappingStack extends MappingStack {

	public ExistingMappingStack(ServerCoreModule module, SyncEntity entity,
			ServerEntityData data, MessageConnection connection) {
		super(module, entity, data, module.getData(connection));
	}

	@Override
	public boolean exists() {
		return true;
	}

	@Override
	public ExistingMappingStack create() {
		return this;
	}

	public ExistingMappingStackElement getExistingTop() {
		return (ExistingMappingStackElement) getTop();
	}

	protected void register() {
		entityData.addMapping(module, connectionData, getTop());
		connectionData.addMapping(entityData, this);
		getExistingTop().notifyInit();
	}

	@Override
	public MappingStack destroy() {
		return destroy(getExistingTop());
	}

	public MappingStack destroy(ExistingMappingStackElement last) {
		while (getBottom().exists()) {
			getBottom().destroy();
		}
		entityData.removeMapping(connectionData, last);
		connectionData.removeMapping(entityData, this);
		return module.getDummyStack();
	}

}
